package com.lxp.kotlintest.view.news

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.lxp.kotlintest.R
import com.lxp.kotlintest.adapter.FunctionAdapter
import com.lxp.kotlintest.base.BaseActivity
import com.lxp.kotlintest.bean.news.Data
import com.lxp.kotlintest.bean.news.NewsInfo
import com.lxp.kotlintest.contract.NewsContract
import com.lxp.kotlintest.presenter.NewsPresenter
import kotlinx.android.synthetic.main.activity_news_list.*

class NewsListActivity : BaseActivity(), NewsContract.View {

    var mData = mutableListOf<Data>()

    var mPresenter = NewsPresenter()

    override fun bindLayout(): Int {
        return R.layout.activity_news_list
    }

    override fun initParms(parms: Bundle?) {
    }

    override fun initViews() {
        mPresenter.attachView(this)
        toolbar.title = "新闻列表"
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = FunctionAdapter(mData)

    }

    override fun doBusiness() {
        mPresenter.getNewsList()
    }

    override fun getNewsList(newsInfo: NewsInfo) {
        mData.clear()
        mData.addAll(newsInfo.result.data)
        recyclerView.adapter?.notifyDataSetChanged()

    }

    override fun showError(errorMsg: String, errorCode: Int, errorLevel: Int) {
        layout_parent.showEmpty()
    }

    override fun showLoading() {
        layout_parent.showLoading()
    }

    override fun dismissLoading() {
        layout_parent.showContent()
    }


    override fun widgetClick(v: View?) {

    }


    override fun onDestroy() {
        super.onDestroy()
        mPresenter.detachView()

    }


}
